package com.zklh.leetcode.top100;

/**
 * 搜索二维矩阵II算法实现
 *
 * @version 1.0
 * @Author tongnan
 * @Date 2023/10/9
 */
public class Solution240 {
    public boolean searchMatrix(int[][] matrix, int target) {
        int x = 0, y = matrix[0].length-1;
        int xLimit = matrix.length - 1;
        int yLimit = matrix[0].length - 1;
        while (x >= 0 && x <= xLimit && y >= 0 && y <= yLimit) {
            if (matrix[x][y] == target) {
                return true;
            }
            if (matrix[x][y] < target) {
                x += 1;
            } else if (matrix[x][y] > target){
                y-=1;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[][] matrix = {{1,4,7,11,15},{2,5,8,12,19},{3,6,9,16,22},{10,13,14,17,24},{18,21,23,26,30}};
        Solution240 solution240 = new Solution240();
        System.out.println(solution240.searchMatrix(matrix, 5));
    }
}